package com.fatesgo.controller;

import com.fatesgo.util.RedisUtil;
import com.sun.deploy.cache.BaseLocalApplicationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.fatesgo.service.ILoginService;
import com.fatesgo.util.Response;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @Autowired
    RedisUtil redisUtil;
    /**
     * 登录并获取token
     */
    @PostMapping("/login")
    public Response login(@RequestBody JSONObject userObj) {
        return loginService.login(String.valueOf(userObj.get("username")), String.valueOf(userObj.get("password")));
    }

    @PostMapping("/logout")
    public Response logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> user = (Map<String, Object>) redisUtil.get("user_info" + token);
        if(user==null||user.get("id")==null){
            return Response.success("成功");
        }
        return loginService.logout(token, (String) user.get("id"));
    }
}
